1276A - As Simple as One and Two - CodeForces Solution


dp greedy *1400

Please click on ads to support us..

Python Code:

t=int(input())
for i in range(t):
    s=input()
    c=0
    ind=0
    n=len(s)
    lsta=[]
    while True:
        if ind> n-1:
            break
        else:
            if s[ind:ind+5]=="twone":
                c+=1
                lsta.append(ind+3)
                ind+=5
            elif s[ind:ind+3]=="two":
                c+=1
                lsta.append(ind+2)
                ind+=3
            elif s[ind:ind+3]=="one":
                c+=1
                lsta.append(ind+2)
                ind+=3
            else:
                ind+=1
    print(c)
    print(*lsta)

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define  fast ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
 
int main()
{
	fast;	
	long long  int t;
	string st;
	cin>>t;
	while(t--){
		cin>>st;
		long long int j=0;
		long long int d=0;
		long long int i=0;
		long long int n=st.length();
		bool test=false;
		vector<long long int>s1;
		vector<long long int>s2;
		vector<long long int>s3;
		vector<long long int>s4;
		vector<long long int>s;
		long long int som=0;
		while(i<st.length()){
			if(st[i]=='w'){
				if(test==true){
					som+=1;
					s.insert(s.end(),(s2[0]-1));
				s1.clear();s2.clear();
				test=false;}
				 j=i-1;
				 d=i+1;
				while((j>=0)and(st[j]=='t')){
					s1.push_back(j+1);
					j--;}
				while((d<n)and(st[d]=='o')){
					s2.push_back(d+1);
					d++;}
			if(s1.empty()or(s2.empty())){
				s2.clear();
				s1.clear();
				i=d;
				continue;}
			else if(d==n){
				som+=1;
				s.insert(s.end(),(s2[0]-1));
				s1.clear();s2.clear();
				test=false;
				break;}
			else{
				i=d;test=true;}
			}
			else if(st[i]=='n'){
				 j=i-1;
				 d=i+1;
				while((j>=0)and(st[j]=='o')){
					s3.push_back(j+1);
					j--;}
				while((d<n)and(st[d]=='e')){
					s4.push_back(d+1);
					d++;}
			if(s3.empty()or(s4.empty())){
				s3.clear();
				s4.clear();
				i=d;
				if(test==true){som+=1;
				s.insert(s.end(),(s2[0]-1));
				s1.clear();s2.clear();}
				test=false;
				continue;}
			else if	(test==true){
				if(s3.size()==1){
					som+=1;
					s.insert(s.end(),(s3[0]));
					i=s3[0]+1;
					s3.clear();s1.clear();s4.clear();s2.clear();}
				else{
				som+=2;
				s.insert(s.end(),(s2[0]-1));
				s.insert(s.end(),(s4[0]-1));i=s4[0];
				s1.clear();s2.clear();s4.clear();s3.clear();
				test=false;
				}}
			else{
					som+=1;
					s.insert(s.end(),s4[0]-1);
				s3.clear();s4.clear();}
				i=d;test=false;
				}
			else if(test==true){
				som+=1;
				s.insert(s.end(),(s2[0]-1));
				s1.clear();s2.clear();
				s2.clear();s1.clear();
				test=false;
				i++;}
			else{i++;}
		
	}
		cout<<som<<endl;
		for(long long int i(0);i<s.size();i++)cout<<s[i]<<" ";
		cout<<endl;
		
		
		
		}
	
	
	
		
	
		
		
		
		
		
		
 
 
	
		
		
		
		
 
		
		
	
		
	
		
		
		
	
 
 
	
 
 
 
	
	
	
	
	
 
	
	
	
	
	
 
	
 
	
	
		
	
	
	
	
	
	
return 0;}


Comments

Submit
0 Comments
More Questions

Two Strings
Anagrams
Prime Number
Lexical Sorting Reloaded
1514A - Perfectly Imperfect Array
580A- Kefa and First Steps
1472B- Fair Division
996A - Hit the Lottery
MSNSADM1 Football
MATCHES Playing with Matches
HRDSEQ Hard Sequence
DRCHEF Doctor Chef
559. Maximum Depth of N-ary Tree
821. Shortest Distance to a Character
1441. Build an Array With Stack Operations
1356. Sort Integers by The Number of 1 Bits
922. Sort Array By Parity II
344. Reverse String
1047. Remove All Adjacent Duplicates In String
977. Squares of a Sorted Array
852. Peak Index in a Mountain Array
461. Hamming Distance
1748. Sum of Unique Elements
897. Increasing Order Search Tree
905. Sort Array By Parity
1351. Count Negative Numbers in a Sorted Matrix
617. Merge Two Binary Trees
1450. Number of Students Doing Homework at a Given Time
700. Search in a Binary Search Tree
590. N-ary Tree Postorder Traversal